package com.add.Crack;

import com.add.L1Config_other;
import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.L1SpawnUtil;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;

public class L1Thebes {
  private static Logger _log = Logger.getLogger(L1Thebes.class.getName());
  
  public static final int STATUS_NONE = 0;
  
  public static final int STATUS_READY = 1;
  
  public static final int STATUS_PLAYING = 2;
  
  public static final int STATUS_AGAIN = 3;
  
  public static final int STATUS_END = 4;
  
  private static ArrayList<L1PcInstance> playerList = new ArrayList<>();
  
  public static int _status = 0;
  
  private static final int END_STATUS_WINNER = 1;
  
  private static final int END_STATUS_NOWINNER = 2;
  
  public static int _type = 0;
  
  public static void enterGame(L1PcInstance pc, int npcid) {
    L1Object obj = World.get().findObject(npcid);
    L1NpcInstance npc = (L1NpcInstance)obj;
    if (!L1CrackTime.getStart().getGateOpen()) {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "thebegate2"));
      return;
    } 
    if (!pc.getInventory().checkItem(49242, 1L)) {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "thebegate3"));
      return;
    } 
    if (getGameStatus() == 2) {
      if (playerList.contains(pc)) {
        L1Teleport.teleport(pc, 32734, 32832, (short)782, 2, true);
      } else {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("副本已开始，无法进入。"));
      } 
      return;
    } 
    if (getGameStatus() == 3) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("副本正在重置中。"));
      return;
    } 
    if (getGameStatus() == 4) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("BOSS已死亡，无法进入。"));
      return;
    } 
    if (playerList.size() >= L1Config_other._2213) {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "thebegate4"));
      return;
    } 
    if (getGameStatus() == 0) {
      L1Teleport.teleport(pc, 32734, 32832, (short)782, 2, true);
      pc.getInventory().consumeItem(49242, 1L);
      addPlayerList(pc);
    } 
    if (getGameStatus() == 1 && playerList.size() < L1Config_other._2213) {
      L1Teleport.teleport(pc, 32734, 32832, (short)782, 2, true);
      pc.getInventory().consumeItem(49242, 1L);
      addPlayerList(pc);
    } 
  }
  
  private static void addPlayerList(L1PcInstance pc) {
    if (!playerList.contains(pc))
      playerList.add(pc); 
    if (playerList.size() == L1Config_other._2211) {
      if (getGameStatus() == 0) {
        setGameStatus(1);
        Iterator<L1Object> iterator = World.get().getVisibleObjects(782).values().iterator();
        while (iterator.hasNext()) {
          Object obj = iterator.next();
          if (obj instanceof L1PcInstance) {
            L1PcInstance tgpc = (L1PcInstance)obj;
            tgpc.sendPackets((ServerBasePacket)new S_SystemMessage("时空裂痕副本将在5秒后开始。"));
          } 
        } 
        OverTime(5);
      } 
    } else if (playerList.size() < L1Config_other._2211) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("需要有至少 " + L1Config_other._2211 + " 人以上参加者才可能开始副本。"));
    } 
  }
  
  public static void removePlayerList(L1PcInstance pc) {
    if (playerList.contains(pc)) {
      playerList.remove(pc);
      L1Teleport.teleport(pc, 33442, 32797, (short)4, 5, true);
    } 
  }
  
  public static void setGameStatus(int i) {
    _status = i;
  }
  
  private static int getGameStatus() {
    return _status;
  }
  
  private static void setGameEnd(int type) {
    switch (type) {
      case 1:
        sendEndMessage(type);
        break;
      case 2:
        sendEndMessage(type);
        break;
    } 
    _type = type;
  }
  
  public static int getGameEnd() {
    return _type;
  }
  
  private static void startAgain() {
    (new Again()).begin();
  }
  
  private static void startAgain2() {
    (new Again2()).begin();
  }
  
  private static void sendEndMessage(int type) {
    if (type == 1) {
      Iterator<L1PcInstance> iterator = playerList.iterator();
      while (iterator.hasNext()) {
        L1PcInstance pc = iterator.next();
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1474));
      } 
    } else if (type == 2) {
      Iterator<L1PcInstance> iterator2 = playerList.iterator();
      while (iterator2.hasNext()) {
        L1PcInstance pc = iterator2.next();
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1479));
      } 
    } 
    Overtime2(type);
  }
  
  private static void Overtime2(int type) {
    int time = 40;
    try {
      while (time >= 0) {
        Thread.sleep(1000L);
        Iterator<L1PcInstance> iterator = playerList.iterator();
        while (iterator.hasNext()) {
          L1PcInstance pc = iterator.next();
          if (time == 35 && type == 1) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1475));
            continue;
          } 
          if (time == 30) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1476));
            continue;
          } 
          if (time == 20) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1477));
            continue;
          } 
          if (time == 10) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1478));
            continue;
          } 
          if (time == 5) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1480));
            continue;
          } 
          if (time == 4) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1481));
            continue;
          } 
          if (time == 3) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1482));
            continue;
          } 
          if (time == 2) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1483));
            continue;
          } 
          if (time == 1) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1484));
            continue;
          } 
          if (time != 0 || type != 2)
            continue; 
          setGameStatus(3);
          playerList.clear();
          startAgain();
        } 
        time--;
      } 
    } catch (Exception e) {
      _log.warning(e.getMessage());
    } 
  }
  
  public static void OverTime(int time) {
    try {
      while (time >= 0) {
        Thread.sleep(1000L);
        time--;
        Iterator<L1PcInstance> iterator = playerList.iterator();
        while (iterator.hasNext()) {
          L1PcInstance pc = iterator.next();
          if (time == 4) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1471));
            continue;
          } 
          if (time == 3) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1472));
            continue;
          } 
          if (time != 2)
            continue; 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(1473));
        } 
        if (time == 1) {
          L1SpawnUtil.spawn(46124, 32781, 32826, (short)782, 0);
          L1SpawnUtil.spawn(46123, 32781, 32837, (short)782, 0);
          startSeekBossSecond();
        } 
      } 
    } catch (Exception e) {
      _log.warning(e.getMessage());
    } 
  }
  
  private static void startSeekBossSecond() {
    (new SeekBossSecond()).begin();
  }
  
  private static int MobCount(short mapId) {
    int MobCount = 0;
    Iterator<L1Object> iterator = World.get().getVisibleObjects(mapId).values().iterator();
    while (iterator.hasNext()) {
      Object obj = iterator.next();
      if (obj instanceof L1MonsterInstance) {
        L1MonsterInstance mob = (L1MonsterInstance)obj;
        if (mob == null || mob.isDead())
          continue; 
        MobCount++;
      } 
    } 
    return MobCount;
  }
  
  private static int PcCount(short mapId) {
    int PcCount = 0;
    Iterator<L1Object> iterator = World.get().getVisibleObjects(mapId).values().iterator();
    while (iterator.hasNext()) {
      Object obj = iterator.next();
      if (obj instanceof L1PcInstance) {
        L1PcInstance pc = (L1PcInstance)obj;
        if (pc == null || pc.isDead())
          continue; 
        PcCount++;
      } 
    } 
    return PcCount;
  }
  
  private static class Again extends TimerTask {
    private Again() {}
    
    public void run() {
      L1Thebes.setGameStatus(0);
      Iterator<L1Object> iterator = World.get().getVisibleObjects(782).values().iterator();
      while (iterator.hasNext()) {
        Object obj = iterator.next();
        if (obj instanceof L1NpcInstance) {
          L1NpcInstance npc = (L1NpcInstance)obj;
          npc.deleteMe();
        } 
      } 
      cancel();
    }
    
    public void begin() {
      Timer timer = new Timer();
      timer.schedule(this, (L1Config_other._2212 * 60 * 1000));
    }
  }
  
  private static class Again2 extends TimerTask {
    private Again2() {}
    
    public void run() {
      L1Thebes.setGameStatus(0);
      Iterator<L1Object> iterator = World.get().getVisibleObjects(782).values().iterator();
      while (iterator.hasNext()) {
        Object obj = iterator.next();
        if (obj instanceof L1NpcInstance) {
          L1NpcInstance npc = (L1NpcInstance)obj;
          npc.deleteMe();
        } 
      } 
      cancel();
    }
    
    public void begin() {
      Timer timer = new Timer();
      timer.schedule(this, (L1Config_other._2218 * 60 * 1000));
    }
  }
  
  private static class SeekBossSecond extends TimerTask {
    private SeekBossSecond() {}
    
    public void run() {
      if (L1Thebes.MobCount((short)782) == 0) {
        L1Thebes.setGameEnd(1);
      } else if (L1Thebes.PcCount((short)782) == 0) {
        L1Thebes.setGameEnd(2);
      } else {
        L1Thebes.startSeekBossSecond();
      } 
      cancel();
    }
    
    public void begin() {
      Timer timer = new Timer();
      timer.schedule(this, 5000L);
    }
  }
}
